package shop.service;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import shop.ItemInfo;
import shop.dao.ItemInfoDaoAware;
import shop.dao.ShopAware;
import shop.dao.Shop_ItemDaoAware;
import shop.dao.impl.ItemInfoDao;
import shop.dao.impl.ShopDao;
import shop.dao.impl.Shop_ItemDao;
import shop.util.DBUtils;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class Service {

	private ItemInfoDaoAware itemInfoDao = new ItemInfoDao();
    private Shop_ItemDaoAware sho_itemDao = new Shop_ItemDao(); 
    private ShopAware shopDao = new ShopDao();
    
	/*
	 * 根据Id号查询产品信息
	 */
	public List getShopItemInfo(int i) throws Exception{

		List<Integer> list = sho_itemDao.getItemsByShopId(i);
		return itemInfoDao.getItemsInfoByIds(list);
	}
	
	public List getShopItemInfoByShopName(String shopname) {
		
		System.out.println("验证参数：" + shopname );
		String name=this.spiltShopName(shopname);
		List<Integer> list = sho_itemDao.getShopItemInfoByShopName(name);
	
		return itemInfoDao.getItemsInfoByIds(list);
	}
	
	
	//======================================================
	
	/**
	 * 更新顾客的钱包
	 */
    public void updateCustomerAccountMoney(double money,String shopname){
    	shopDao.updateShopMomey(money, shopname);
    }
    
    /**
	 * 更新商店的商品数量
	 */
    
    public void updateItemQuantityByItemName(List<ItemInfo> Catalog){
    	System.out.println("shop.service.Service 更新商店的商品数量");
    	shopDao.updateItemQuantityByItemName(Catalog);
    }
    
    /**
     * 买商品，刷新数据 在CustomerPannel 上调用
     */
    public void buyItemFromShop(Object object , String itemName , double ItemPrice){
    	
    	String shopname  = String.valueOf(object);
    	int index=shopname.lastIndexOf(",");
    	shopname = shopname.substring(0, index);
    	System.out.println("shop.service.Service  输出商店名称 并初始化 商店钱数 从数据库获取 ：" + shopname);
    	
    	System.out.println(" shop.Service() 商店名：" + shopname + " 商品名："+itemName + " 商品价格" + ItemPrice);
    	/*
    	 * 先在商品数量先减
    	 *商店钱数加
    	 */
    	int quality= this.getItemInfoQalityByName(itemName);
        this.updateItemInfoQualityByName(itemName, quality-1);
        //商店钱数加
        double shopMoney = this.getShopMoneybyShopName(shopname);
        this.updateShopMoney(shopMoney+ItemPrice, shopname);
        
    }
    
    ///////////////////////////  ShopDao  //////////////////////////////////
    
    public double getShopMoneybyShopName(String shopname){
    	double result= shopDao.getShopMoneyByShopName(shopname);
		return result;
    }
	/**
	 * 更新商店卖出的商品收获的钱
	 */
    public void updateShopMoney(double money,String shopname){
    	System.out.println("更新数据  当前的钱:"+money + " 商店名："+ shopname);
    	shopDao.updateShopMomey(money, shopname);
    }
    
	/**
	 * 更新商店卖出的商品收获的钱
	 */
    public void updateShopAccountMoney(double money,String shopname){
    	int index=shopname.lastIndexOf(",");
    	String str=shopname.substring(0, index);
    	System.out.println("shop.service.Service 更新商店的钱数  " + " 钱:"+money+" 商店名："+shopname);
    	shopDao.updateShopMomey(money, str);
    }
    
    
    /////////////////////////////  ItemInfoDao  ///////////////////////////////////////////
    
    /**
     * 
     * @param itemName
     * @return
     */
    public int getItemInfoQalityByName(String itemName){
    	return itemInfoDao.getItemInfoQualityByName(itemName);
    }
    public void updateItemInfoQualityByName(String itemName , int quality) {
    	 System.out.println("输出数量：" + quality);
    	 itemInfoDao.updateItemInfoQualityByName(itemName,quality);
    }
    
    //=================  add 业务  ============================================
    
    /**
     * add 业务
     * @param iteminfo
     * @param shopName
     */
    public void addItemInfoByShopName(ItemInfo iteminfo , String shopName ) {
    	
    	System.out.println(" add 验证一下参数："+shopName+ " 商品名  "+iteminfo.getName());
    	//第一步：插入数据itemInfo
    	//第二步：获取新的iteminfo的全部信息
    	//第三步：获取shop的全部信息
    	//第四步：把全部信息写到关联表
    	
    	///////////////////////////////////////////
    	
    	itemInfoDao.addItemInfo(iteminfo);
    	Item item = itemInfoDao.getItemInfoByName(iteminfo.getName());
    	Shop shop = shopDao.getShopInfoByName(this.spiltShopName(shopName));
    	
    	sho_itemDao.add_shop_Item(shop, item);
    }
    
    /**
     * edit 业务  
     * @param iteminfo
     * @param shopName
     */
    public void editItemInfoByShopName(ItemInfo iteminfo , ItemInfo oldIteminfo, String shopName ){
    	System.out.println(" edit 验证一下参数 商店名："+shopName+ " 新商品名  "+iteminfo.getName());
   	 System.out.println(" edit 验证一下参数 商店名："+shopName+ " 旧商品名  "+oldIteminfo.getName());
    	 String shopname=this.spiltShopName(shopName);
    	 System.out.println(this.spiltShopName(shopName));
    	//第一步：根据商店名，获取商店信息，商店ID
     	//第二步：通过 商店——商品表找到 商品ID;根据  旧商店名 和  旧商品名：
     	//第三步：修改更新商品表数据
     	//第四步：更新 商店-商品的 数据：
    	 
    	 //////////////////////////////////////////////////////
    	 Item olditem =new Item();
    	 Item newitem =new Item();
    	 Shop shop = shopDao.getShopInfoByName(shopname);
    	 //根据旧商品名和 商店名 获取 商品ID
    	 int olditemId= sho_itemDao.get_ItemID_From_shopItemInfoByShopNameAndItemName(shopname,oldIteminfo.getName());
    	 olditem.setId(olditemId);
    	 olditem.setName(oldIteminfo.getName());
    	 olditem.setPrice(oldIteminfo.getPrice());
    	 olditem.setQuantity(oldIteminfo.getQuantity());
    	 
    	 /////////////////
    	 newitem.setId(olditemId);
    	 newitem.setName(iteminfo.getName());
    	 newitem.setPrice(iteminfo.getPrice());
    	 newitem.setQuantity(iteminfo.getQuantity());
    	 
    	 
    	 itemInfoDao.updateItemInfo(newitem);
    	 
    	 //////////
    	 sho_itemDao.update_shop_item(shop, newitem);
    	 
    }
    /**
     * 移除数据  remove
     * @param iteminfo
     * @param shopName
     */
    public void removeItemInfoByShopName(ItemInfo iteminfo , String shopName ){
    	 System.out.println(" edit 验证一下参数："+shopName+ " 商品名  "+iteminfo.getName());
	   	 String shopname=this.spiltShopName(shopName);
	   	 System.out.println(this.spiltShopName(shopName));
	   	 
	   //第一步：去关联表，获取实体表Id,根据商店名和商品名；
	   	 //第二步：去商店表，获取商店信息，根据商店名
	     //第三步：删除实体表，根据实体表Id
	   	 //第四步：删除关联表，根据商店ID,实体ID
    	 Item item =new Item();
    	 int itemId= sho_itemDao.get_ItemID_From_shopItemInfoByShopNameAndItemName(shopname,iteminfo.getName());
    	 item.setId(itemId);
    	 item.setName(iteminfo.getName());
    	 item.setPrice(iteminfo.getPrice());
    	 item.setQuantity(iteminfo.getQuantity());
    	 
    	 ///获取商店信息
    	 Shop shop = shopDao.getShopInfoByName(shopname);
    	 
    	 //remove
    	 itemInfoDao.removeItemInfoById(item.getId());
    	 sho_itemDao.remove_From_shopItemInfoByShopIdAndItemId(shop.getId(), item.getId());
	   	 
    }
    
    
    ///////////////////////////////////////////////////////////////
    /*====================  util  =======================*/
    
    private String spiltShopName(String shopname){
    	int index=shopname.lastIndexOf(",");
    	String str=shopname.substring(0, index);
    	return str;
    }
    
    
    
}
